package com.mid;

/**
 * Created by Lxk on 2020/2/3.
 */
public class Solution50 {

    private double powCal(double x, int n) {
        boolean flag = true;
        if (n < 0) {
            flag = false;
            n = Math.abs(n);
        }
        double res = 1;
        int count = 0;
        while (count < n) {
            res *= x;
            count++;
        }
        if (!flag) {
            return 1 / res;
        } else {
            return res;
        }
    }

    private double powCal2(double x, int n) {
        if (n == 0) {
            return 1;
        }
        double res = powCal2(x, n / 2);
        if (n % 2 == 0) {
            return res * res;
        } else {
            return res * res * x;
        }
    }

    public double myPow(double x, int n) {
        if (n < 0) {
            x = 1 / x;
            n = (-n);
        }
        return powCal2(x, n);
    }

    public static void main(String[] args) {
        Solution50 solution50 = new Solution50();
        System.out.println(solution50.myPow(2.0, -2));
    }

}
